package com.elinkway.infinitemovies.ui.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.elinkway.infinitemovies.ui.activity.play.PlayerUtils;
import com.elinkway.infinitemovies.utils.DialogUtil;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.NetWorkTypeUtils;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

public abstract class BaseLoadingActivity extends BaseActionBarActivity {
    private String TAG = "BaseLoadingActivity";

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    private View loading;
    private ImageView mLoadingIv;
    private AnimationDrawable animationDrawable;
    private View loading_failed;
    private Button btnRefresh;
    private ViewGroup loading_root;
    //网络变化广播接收
    private NetworkCheckReceiver mCheckReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loading_root = (ViewGroup) UIs.inflate(this, R.layout.activity_base_loading, null);
        setContentView(loading_root);
        initLoadingView();
        initActionBar();
    }

    @Override
    protected void initActionBar() {
        super.initActionBar();
    }

    protected void setLoadingContentView(int layoutId) {
        UIs.inflate(this, layoutId, loading_root);
    }

    protected void initLoadingView() {
        loading = loading_root.findViewById(R.id.base_loading_loading);
        mLoadingIv = (ImageView) loading_root.findViewById(R.id.loading_progressBar);
        loading_failed = loading_root.findViewById(R.id.base_loading_failed);
        btnRefresh = (Button) loading.findViewById(R.id.bn_refresh);
    }

    protected void setLoadingFailed() {
        loading.setVisibility(View.GONE);
        animationDrawable = (AnimationDrawable) mLoadingIv.getDrawable();
        animationDrawable.stop();
        loading_failed.setVisibility(View.VISIBLE);
        btnRefresh = (Button) loading_failed.findViewById(R.id.bn_refresh);
        btnRefresh.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                onLoadingRefresh();
                onLoading();
            }
        });
        onLoadingFailed();
    }

    protected void setOnLoading() {
        loading.setVisibility(View.VISIBLE);
        animationDrawable = (AnimationDrawable) mLoadingIv.getDrawable();
        animationDrawable.start();
        loading_failed.setVisibility(View.GONE);
        onLoading();
    }

    protected void setLoadingComplete() {
        loading.setVisibility(View.GONE);
        animationDrawable = (AnimationDrawable) mLoadingIv.getDrawable();
        animationDrawable.stop();
        loading_failed.setVisibility(View.GONE);
        onLoadingComplete();
    }

    protected abstract void onLoadingRefresh();

    protected abstract void onLoading();

    protected abstract void onLoadingFailed();

    protected abstract void onLoadingComplete();


    /**
     * qinguoli
     * 注册监听手机网络广播 接收
     */
    protected void registerCheckNetwork() {
        mCheckReceiver = new NetworkCheckReceiver();
        IntentFilter intentfilter = new IntentFilter();
        intentfilter.addAction(PlayerUtils.CONNECTIVTY_CHANGE);
        intentfilter.addAction(Utils.SDCARD_NOSPACE_ACTION);
        this.registerReceiver(mCheckReceiver, intentfilter);
    }

    /**
     * qinguoli
     * 取消对网络变化的监听*
     */
    protected void unregisterCheckNetwork() {
        if (mCheckReceiver != null) {
            try {
                this.unregisterReceiver(mCheckReceiver);
            } catch (IllegalArgumentException e) {
                if (e.getMessage().contains("Receiver not registered")) {
                    LogUtils.e(TAG, "!!!!!!!!!!!!!!!!!ignore incorrect unregisterCheckNetwork!!!!!!!!!!!!!!!!!");
                    // Ignore this exception. This is exactly what is desired
                } else {
                    // unexpected, re-throw
                    throw e;
                }
            }
        }
    }

    /**
     * qinguoli
     * 网络变化的监听
     */
    class NetworkCheckReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (intent.getAction().equals(PlayerUtils.CONNECTIVTY_CHANGE)) {
                    //读取保存的之前网络状态
                    SharedPreferences priorNetState = getSharedPreferences("priornetstate",
                            Activity.MODE_PRIVATE);

                    int priorNetType = priorNetState.getInt("netstate", 2);

                    int nowNetType = NetWorkTypeUtils.checkNet(intent, priorNetType);


                    SharedPreferences.Editor editor = priorNetState.edit();

                    editor.putInt("netstate", nowNetType);
                    editor.commit();
                }
                if (intent.getAction().equals(Utils.SDCARD_NOSPACE_ACTION)) {
                    DialogUtil.getInstance(BaseLoadingActivity.this).checkSDCardSpaceDialog();
                }


            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerCheckNetwork();
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterCheckNetwork();
    }

}
